package com.igeek.boot.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.igeek.boot.entity.Incomingstock;
import com.igeek.boot.entity.Outgoingstock;
import com.igeek.boot.mapper.IncomingstockMapper;
import com.igeek.boot.mapper.OutgoingstockMapper;
import com.igeek.boot.service.IIncomingstockService;
import com.igeek.boot.service.IOutgoingstockService;
import com.igeek.boot.service.IReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @package com.igeek.boot.service.impl
 * @Description POI报表业务
 * @Author LittlePunk [296526219@qq.ccom]
 * @Version 2022.1
 * @Date 2024/1/10 23:23
 */
@Service
public class ReportServiceImpl implements IReportService {

    @Autowired
    private IncomingstockMapper incomingstockMapper;

    @Autowired
    private OutgoingstockMapper outgoingstockMapper;

    //查询报表数据
    @Override
    public Map<String, Object> selectBusinessData() {

        //Date now = DateUtil.parse(DateUtil.now(), "yyyy-MM-dd");
        // 获取当前日期
        // 获取当前日期
        LocalDateTime now = LocalDateTime.now();
        //今天开始
        LocalDateTime today = LocalDate.now().atStartOfDay();
        //今天结束
        LocalDateTime todayEnd = LocalDate.now().atTime(23, 59, 59);

        //一周开始
        LocalDateTime firstDayOfWeek = LocalDate.now().with(DayOfWeek.MONDAY).atStartOfDay();
        //一周结束
        LocalDateTime lastDayOfWeek = LocalDate.now().with(DayOfWeek.MONDAY).plusDays(6).atTime(23,59,59);
        //本周第一天
        //Date beginOfWeek = DateUtil.beginOfWeek(now);
        //本周最后一天
        //Date endOfWeek = DateUtil.endOfWeek(now);
        //本月第一天
        //Date beginOfMonth = DateUtil.beginOfMonth(now);
        //本月最后一天
        //Date endOfMonth = DateUtil.endOfMonth(now);

        //# 今日新增入库订单数
        //select count(*) from incomingstock where incoming_date between '2024-01-06' and '2024-01-09'
        Long totayInStockOrder = incomingstockMapper.selectCount(new LambdaQueryWrapper<Incomingstock>()
                .between(Incomingstock::getIncomingDate, today, todayEnd));

        //# 今日完成入库订单数
        //select count(*) from incomingstock where update_time between '2024-01-06' and '2024-01-07' and station=1
        Long totayIncomingStockOrder = incomingstockMapper.selectCount(new LambdaQueryWrapper<Incomingstock>()
                .between(Incomingstock::getUpdateTime, today, todayEnd)
                .eq(Incomingstock::getStation, 1));

        //#本周新增入库订单数
        //select count(*) from incomingstock where incoming_date between '2024-01-03' and '2024-01-09'
        Long thisWeekInStockOrder = incomingstockMapper.selectCount(new LambdaQueryWrapper<Incomingstock>()
                .between(Incomingstock::getIncomingDate, firstDayOfWeek, lastDayOfWeek));

        //#本周新增入库订单数
        //select count(*) from incomingstock where update_time between '2024-01-03' and '2024-01-15' and station=1
        Long thisWeekIncomingStockOrder = incomingstockMapper.selectCount(new LambdaQueryWrapper<Incomingstock>()
                .between(Incomingstock::getUpdateTime, firstDayOfWeek, lastDayOfWeek)
                .eq(Incomingstock::getStation, 1));

        //#总入库订单
        //select count(*) from incomingstock
        Long totalInStock = incomingstockMapper.selectCount(null);

        //#总已入库订单
        //select count(*) from incomingstock where station=1
        Long totalIningStock = incomingstockMapper.selectCount(new LambdaQueryWrapper<Incomingstock>()
                .eq(Incomingstock::getStation, 1));


        //# 今日新增出库订单统计
        //select count(*) from outgoingstock where outgoing_date between '2024-01-06' and '2024-01-07'
        Long todayOutStockOrder = outgoingstockMapper.selectCount(new LambdaQueryWrapper<Outgoingstock>()
                .between(Outgoingstock::getOutgoingDate, today, todayEnd));
        //# 今日已出库订单统计
        //select count(*) from outgoingstock where update_time between '2024-01-07' and '2024-01-08' and station=1
        Long todayOutgoingStockOrder = outgoingstockMapper.selectCount(new LambdaQueryWrapper<Outgoingstock>()
                .between(Outgoingstock::getUpdateTime, today, todayEnd)
                .eq(Outgoingstock::getStation, 1));

        //# 本周新增出库订单统计
        //select count(*) from outgoingstock where outgoing_date between '2024-01-06' and '2024-01-07'
        Long thisWeekOutStockOrder = outgoingstockMapper.selectCount(new LambdaQueryWrapper<Outgoingstock>()
                .between(Outgoingstock::getOutgoingDate, firstDayOfWeek, lastDayOfWeek));
        //# 本周已出库订单统计
        //select count(*) from outgoingstock where update_time between '2024-01-07' and '2024-01-08' and station=1
        Long thisWeekOutgoingStockOrder = outgoingstockMapper.selectCount(new LambdaQueryWrapper<Outgoingstock>()
                .between(Outgoingstock::getUpdateTime, firstDayOfWeek, lastDayOfWeek)
                .eq(Outgoingstock::getStation, 1));
        //# 总新增出库订单统计
        //select count(*) from outgoingstock
        Long totalOutStock = outgoingstockMapper.selectCount(null);
        //# 总已出库订单统计
        //select count(*) from outgoingstock where station=1
        Long totalOutgoingStock = outgoingstockMapper.selectCount(new LambdaQueryWrapper<Outgoingstock>()
                .eq(Outgoingstock::getStation, 1));

        //# 物资名称  入库数量  入库占比
        //SELECT incoming_name, SUM(incoming_quantity) / (SELECT SUM(incoming_quantity) FROM incomingstock WHERE station = 1) AS ratio FROM incomingstock WHERE station = 1 GROUP BY incoming_name ORDER BY ratio DESC LIMIT 0, 4;
        List<Map<String,Object>> list1 = incomingstockMapper.selectHotInStock(4);
        //# 物资名称  出库数量 出库占比
        //SELECT outgoing_name, SUM(outgoing_quantity) / (SELECT SUM(outgoing_quantity) FROM outgoingstock WHERE station = 1) AS ratio FROM outgoingstock WHERE station = 1 GROUP BY outgoing_name ORDER BY ratio DESC LIMIT 0, 4;
        List<Map<String,Object>> list2 = outgoingstockMapper.selectHotOutStock(4);

        //封装结果集对象
        Map<String, Object> result = new HashMap<>();
        result.put("reportDate" , DateUtil.format(now , "yyyy/MM/dd"));
        result.put("todayInStockOrder", totayInStockOrder);
        result.put("todayIncomingStockOrder", totayIncomingStockOrder);
        result.put("thisWeekInStockOrder", thisWeekInStockOrder);
        result.put("thisWeekIncomingStockOrder", thisWeekIncomingStockOrder);
        result.put("totalInStock", totalInStock);
        result.put("totalIningStock", totalIningStock);
        result.put("todayOutStockOrder", todayOutStockOrder);
        result.put("todayOutgoingStockOrder", todayOutgoingStockOrder);
        result.put("thisWeekOutStockOrder", thisWeekOutStockOrder);
        result.put("thisWeekOutgoingStockOrder", thisWeekOutgoingStockOrder);
        result.put("totalOutStock", totalOutStock);
        result.put("totalOutgoingStock", totalOutgoingStock);
        result.put("inList", list1);
        result.put("outList", list2);
        return result;
    }

}
